package org.yn.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.sys.service.IDictionaryitemService;
import org.yn.sys.domain.Dictionaryitem;
import org.yn.sys.query.DictionaryitemQuery;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@PermissionAnnotation(name="字典信息管理",descs = "字典信息管理api")
@Api("字典信息管理接口")
@RestController
@RequestMapping("/dictionaryitem")
public class DictionaryitemController {
    @Autowired
    public IDictionaryitemService dictionaryitemService;


    /**
     * 保存和修改公用的
     * @param dictionaryitem  传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name="新增和修改字典信息",descs = "新增和修改字典信息")
    @ApiOperation(value = "新增和修改字典信息")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody Dictionaryitem dictionaryitem){
        try {
            if( dictionaryitem.getId()!=null)
                dictionaryitemService.update(dictionaryitem);
            else
                dictionaryitemService.insert(dictionaryitem);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name="删除字典信息",descs = "删除字典信息")
    @ApiOperation(value = "删除字典信息")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            dictionaryitemService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @PermissionAnnotation(name="获取单个字典信息",descs = "获取单个字典信息")
    @ApiOperation(value = "获取单个字典信息")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id)
    {
        try {
            Dictionaryitem dictionaryitem = dictionaryitemService.selectById(id);
            return AjaxResult.success(dictionaryitem);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @PermissionAnnotation(name="查询全部字典信息",descs = "查询全部字典信息")
    @ApiOperation(value = "查询全部字典信息")
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< Dictionaryitem> list = dictionaryitemService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name="分页查询字典信息",descs = "分页查询字典信息")
    @ApiOperation(value = "分页查询字典信息")
    @PutMapping
    public AjaxResult queryPage(@RequestBody DictionaryitemQuery query)
    {
        try {
            PageInfo<Dictionaryitem> pageInfo = dictionaryitemService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name="批量删除字典信息",descs = "批量删除字典信息")
    @ApiOperation(value = "批量删除字典信息")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
                dictionaryitemService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败"+e.getMessage());
        }
    }

    @GetMapping("/dictionaryitem4")
    public AjaxResult dictionaryitem4(){
        try {
            dictionaryitemService.dictionaryitem4();
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败"+e.getMessage());
        }
    }
    @GetMapping("/dictionary/{id}")
    public AjaxResult selectByDictionaryId(@PathVariable("id") Long id){
        try {
           List<Dictionaryitem> list= dictionaryitemService.selectByDictionaryId(id);
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询dictionaryitemitemByDictionaryId失败"+e.getMessage());
        }
    }
}
